package cc.bukkitPlugin.banitem.command.analyse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.banitem.command.AParamCommand;

public class ParamDamage extends AParam{

    public ParamDamage(BanItem pPlugin,AnalyseMan pAnalyor){
        super(pPlugin,pAnalyor);
        this.mLabels=new String[]{"d","damage"};
    }

    @Override
    public AnalyseResult handle(CommandSender pSender,AnalyseResult pCmdParam,String pArg){
        short tDamage=0;
        try{
            tDamage=Short.parseShort(pArg);
        }catch(NumberFormatException nfexp){
            return pCmdParam.buildError("["+pArg+"]不是正确的Damage值");
        }
        if(tDamage<-1)
            return pCmdParam.buildError("["+pArg+"]不是正确的Damage值");
        pCmdParam.mDamage=tDamage;
        return pCmdParam.buildSuccess();
    }

    @Override
    public ArrayList<String> getHelp(){
        ArrayList<String> help=new ArrayList<>();
        help.add(CFG.cmd_color+"  "+Arrays.toString(this.mLabels));
        help.add(CFG.help_color+"    设置物品的子id值");
        help.add(CFG.help_color+"    值为整数,必须大于等于-1,且在short类型范围内");
        return help;
    }

    @Override
    public ArrayList<String> tabComplete(CommandSender pSender,AParamCommand pCommand,String[] pArgs){
        HashSet<String> list=new HashSet<>();
        if(pArgs==null||pArgs.length==0)
            return new ArrayList<>(list);
        String[] params=pArgs[pArgs.length-1].split(":",2);
        String tLabel=params[0]+":";
        ItemStack itemInHand=null;
        if(pSender instanceof Player){
            itemInHand=((Player)pSender).getItemInHand();
            if(itemInHand!=null&&itemInHand.getType()!=Material.AIR)
                itemInHand=null;
        }
        if(params.length==2&&!params[1].isEmpty()){
            if(params[1].startsWith("-"))
                list.add(tLabel+"-1");
            else if(itemInHand!=null&&(itemInHand.getDurability()+"").startsWith(params[1])){
                list.add(tLabel+params[1]);
            }
        }else{
            if(itemInHand!=null)
                list.add(tLabel+itemInHand.getDurability());
            list.add(tLabel+"0");
            list.add(tLabel+"-1");
        }
        return new ArrayList<>(list);
    }

}
